package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "套餐管理")
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增菜品
     *
     * @param dto
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",key = "#dto.categoryId")
    @ApiOperation("新增菜品")
    @PostMapping
    public Result add(@RequestBody SetmealDTO dto) {
        setmealService.add(dto);
        return Result.success();
    }

    /**
     * 套餐分页查询
     *
     * @param dto
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result page(SetmealPageQueryDTO dto) {
        return Result.success(setmealService.page(dto));
    }

    /**
     * 套餐停售启售
     *
     * @return
     */
    @CacheEvict(cacheNames = "setmeal" ,allEntries = true)
    @ApiOperation("套餐停售启售")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        setmealService.updateStatus(status, id);
        return Result.success();
    }

    /**
     * 根据id查询套餐(回显)
     *
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id) {
        return Result.success(setmealService.findById(id));
    }

    /**
     * 修改套餐
     * @return
     */
    @CacheEvict(cacheNames = "setmeal" ,allEntries = true)
    @ApiOperation("修改套餐")
    @PutMapping
    public Result update(@RequestBody SetmealDTO dto){
        setmealService.upadte(dto);
        return Result.success();
    }

    /**
     * 删除套餐
     * @return
     */
    @CacheEvict(cacheNames = "setmeal" ,allEntries = true)
    @ApiOperation("删除套餐")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        setmealService.delete(ids);
       return Result.success();
    }
}
